﻿// See https://aka.ms/new-console-template for more information
using JsonContractResolverDemo;
using Newtonsoft.Json;

Console.WriteLine("Hello, World!");

var orderModel = new OrderModel()
{
    Id = 1,
    Name = "test",
    OrderItems = new List<OrderItemModel>
    {
        new OrderItemModel()
        {
            Id = 1,
            Name = "test1",
            OrderSubItems = null
        },
        new OrderItemModel()
        {
            Id = 2,
            Name = "test2",
            OrderSubItems = new List<OrderSubItemModel>()
            {
                new OrderSubItemModel()
                {
                    Id= 1,
                    Name= "testc",
                },
                new OrderSubItemModel()
                {
                    Id= 2,
                    Name= "testd",
                }
            }
        },
    }
};
var orderJson = JsonConvert.SerializeObject(orderModel);
Console.WriteLine(orderJson);

var jsonsettings = new JsonSerializerSettings();
jsonsettings.ContractResolver = new IgnoreNullPropertyResolver();
var newOrder = JsonConvert.DeserializeObject<OrderModel>(orderJson, jsonsettings);
var newOrderJson = JsonConvert.SerializeObject(newOrder);
Console.WriteLine(newOrderJson);
Console.ReadLine();